package net.xdclass.xdclass_shiro;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.config.IniSecurityManagerFactory;
import org.apache.shiro.mgt.DefaultSecurityManager;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.realm.SimpleAccountRealm;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.util.Factory;
import org.junit.Before;
import org.junit.Test;

/**
 * @program: xdclass_shiro
 * @description: 单元测试用例执行顺序
 * @author: rZ
 * @create: 2020-01-08 14:59
 **/

public class QuickStartTest5_2 {

    @Test
    public void testAuthentication(){

        //创建SecurityManager工厂，通过配置文件ini创建
        Factory<SecurityManager> factory = new IniSecurityManagerFactory("classpath:shiro.ini");
        SecurityManager securityManager = factory.getInstance();
        //讲SecurityManager 设置到当前运行环境中
        SecurityUtils.setSecurityManager(securityManager);

        Subject subject = SecurityUtils.getSubject();
        //用户输入的账号密码
        UsernamePasswordToken usernamePasswordToken =
                new UsernamePasswordToken("jack","456");
        subject.login(usernamePasswordToken);
        System.out.println("认证结果："+subject.isAuthenticated());
        System.out.println("是否有对应的user角色："+subject.hasRole("user"));
        System.out.println("getPrincipal："+subject.getPrincipal());
        subject.checkPermission("video:find");
        System.out.println("是否有video:find权限："+subject.isPermitted("video:find"));

        subject.logout();
        System.out.println("logout后的认证结果："+subject.isAuthenticated());
    }
}
